package com.achievo.reportserver;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.lang.StringUtils;

import com.achievo.reportserver.config.ReportDef;
import com.achievo.reportserver.config.ReportParam;

/**
 * 
 * Report Server
 * write at Jan 21, 2010
 * @author <a href="mailto:chinaoryx@gmail.com">Hyman.Zhang (Zhang Haipeng)</a>
 * @version 1.0
 * 
 *
 */
public class ReportConfigProcessor implements ReportConstant{
	
	public ReportDef reBuildDef(Map paramsMap, ReportDef reportDef) throws NoSuchAlgorithmException, UnsupportedEncodingException, RuntimeException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchMethodException, ClassNotFoundException{
		
		
		ReportDef newReportDef = new ReportDef();
		BeanUtils.copyProperties(newReportDef, reportDef);
//		newReportDef = (ReportDef) BeanUtils.cloneBean(reportDef);
		// copy the paramList
		List<ReportParam> paramList = reportDef.getParamList();
		List<ReportParam> cloneparamList = new ArrayList<ReportParam>();
		newReportDef.setParamList(cloneparamList);
		if (paramList != null) {
			for (ReportParam reportParam : paramList) {
				ReportParam newParam = new ReportParam();
				BeanUtils.copyProperties(newParam, reportParam);
				newReportDef.addParam(newParam);
			}			
		}
		
		
		Object reportTypeObj = paramsMap.get(PARAM_REPORTTYPE);
		Object reportEngineObj = paramsMap.get(PARAM_ENGINE);
		Object reportIsSyncObj = paramsMap.get(PARAM_ISSYNC);
		String reportType = objToString(reportTypeObj);
		String reportEngine = objToString(reportEngineObj);
		String reportIsSync = objToString(reportIsSyncObj);		
		
		if (!StringUtils.isEmpty(reportType)){
			newReportDef.setReportType(reportType);
		}
		if (!StringUtils.isEmpty(reportEngine)) {
			newReportDef.setReportEngine(reportEngine);
		}
		
		if (!StringUtils.isEmpty(reportIsSync)) {
			newReportDef.setIsSync(reportIsSync);
		}
		
		
		List<ReportParam> newParamList = newReportDef.getParamList();
		if (newParamList != null) {
//			ArrayList<DynaProperty> props = new ArrayList<DynaProperty>();
			for (ReportParam reportParam : newParamList) {
				String paramName = reportParam.getParamName();
				String type = reportParam.getParamType();
				Object paramValue = paramsMap.get(paramName);				
				Converter converter = ConvertUtils.lookup(java.util.Date.class);				
				Object value = ConvertUtils.convert(paramValue, Class.forName(type));
				reportParam.setParamValue(value);				
			}
		}
//		newReportDef.setParamList(newParamList);		
		
		// generate out file name
		String outfilename = null;
		
		
//        BASE64Encoder base64en = new BASE64Encoder();
		outfilename = MD5.encodeString(newReportDef.toString());
		
		if (TYPE_PDF.equalsIgnoreCase(newReportDef.getReportType())) {
			outfilename = outfilename + ".pdf";
		} else if (TYPE_HTML.equalsIgnoreCase(newReportDef.getReportType())){
			outfilename = outfilename + ".html";			
		}		
		newReportDef.setOutFileName(outfilename);
		return newReportDef;
	}
	
	
	
	
	private String objToString(Object obj){
		String str = null;
		if (obj != null) {
			str = obj.toString().trim();
		}
		return str;
	}


	

}
